﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Code.Models;
using Code.BLL;
using QianBuo.Lottery.Web.Models.Account;

namespace QianBuo.Lottery.Web.Areas.Manage.Controllers
{
    [Authorize(Roles = "Manager_Account_Report")]
    public class AccountController : BaseController
    {
        public ActionResult Index(Models.Account.IndexModel model)
        {
            if (model == null)
                model = new Models.Account.IndexModel();
            if (model.Serch == null)
                model.Serch = new UserManage.UserAccountReportSerch();
            if (!model.Page.HasValue || model.Page < 1)
                model.Page = 1;
            if (!model.PageSize.HasValue||model.PageSize<1)
                model.PageSize = CacheManage.OptionSite[Option_Site_Key.Site_PageInfo_PageCount].ToInt32(15);

            var UM = new UserManage();

            var accounts = UM.GetUserAccountReport(model.Serch, null).GroupBy(p => new { p.Year, p.Day, p.Month })
                .Select(p => new UserAccountReport
                {
                    Year = p.Key.Year,
                    Month = p.Key.Month,
                    Day = p.Key.Day,
                    SumAddMoney = p.Sum(pp => pp.SumAddMoney),
                    SumPlayMoney = p.Sum(pp => pp.SumPlayMoney),
                    SumTackMoney = p.Sum(pp => pp.SumTackMoney),
                    SumBackMoney = p.Sum(pp => pp.SumBackMoney),
                    SumBackMoney_Child = p.Sum(pp => pp.SumBackMoney_Child),
                    SumWinMoney = p.Sum(pp => pp.SumWinMoney),
                    SumWinMoney_Child = p.Sum(pp => pp.SumWinMoney_Child)
                });
            model.MimSerchEndTime = (UM.Select<UserAccountDetail, UserAccountDetail>(s => s.OrderBy(p => p.AddTime).FirstOrDefault()) ?? new UserAccountDetail { AddTime = DateTime.Now.Date }).AddTime;
            model.SumCount = accounts.Count();
            if (model.SumCount > 0)
            {
                model.SumReport = accounts.GroupBy(p => 1).Select(p => new AccountReportModel
                {
                    SumAddMoney = p.Sum(pp => pp.SumAddMoney),
                    SumPlayMoney = p.Sum(pp => pp.SumPlayMoney),
                    SumTackMoney = p.Sum(pp => pp.SumTackMoney),
                    SumBackMoney = p.Sum(pp => pp.SumBackMoney),
                    SumBackMoney_Child = p.Sum(pp => pp.SumBackMoney_Child),
                    SumWinMoney = p.Sum(pp => pp.SumWinMoney),
                    SumWinMoney_Child = p.Sum(pp => pp.SumWinMoney_Child)
                }).First();
                model.Reports = accounts.OrderByDescending(p => p.Year).OrderByDescending(p => p.Month).OrderByDescending(p => p.Day)
                    .Select(p => new AccountReportModel
                    {
                        Year = p.Year,
                        Month = p.Month,
                        Day = p.Day,
                        SumAddMoney = p.SumAddMoney,
                        SumPlayMoney = p.SumPlayMoney,
                        SumTackMoney = p.SumTackMoney,
                        SumBackMoney = p.SumBackMoney,
                        SumBackMoney_Child = p.SumBackMoney_Child,
                        SumWinMoney = p.SumWinMoney,
                        SumWinMoney_Child = p.SumWinMoney_Child
                    }).Skip(model.PageSize.Value * (model.Page.Value - 1)).Take(model.PageSize.Value).ToArray();
            }

            return View(model);
        }

    }
}
